package edu.luc.etl.node;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;

public class RemoteNodeProxy implements INode {

	protected InetAddress ip = null;
	protected UUID id;
	protected long lastHeartbeatTime;
	
	/**
	 * 
	 * @param id UUID parseable string
	 * @param ip InetAddress parseable IP address
	 */
	public RemoteNodeProxy(String id, String ip) {
		try {
			this.ip = InetAddress.getByName(ip);
		} catch (UnknownHostException e) { /* Just continue with it set to null. */}
		
		this.id = UUID.fromString(id);
	}

	public RemoteNodeProxy(String uuid, InetAddress address) {
		this.ip = address;
		this.id = UUID.fromString(uuid);
	}

	@Override
	public InetAddress getIp() {
		return ip;
	}

	@Override
	public UUID getId() {
		return id;
	}

	@Override
	public long getLastHeartbeatTime() {
		return lastHeartbeatTime;
	}

	@Override
	public void setLastHeartbeatTime(long lastHeartbeatTime) {
		this.lastHeartbeatTime = lastHeartbeatTime;
	}

	@Override
	public int hashCode() {
		//autogenerated by eclipse
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((ip == null) ? 0 : ip.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object o1) {
		if(o1 instanceof INode) {
			INode node1 = (INode) o1;

			if(this.id != null && this.id.equals(node1.getId()) && 
				this.ip != null && this.ip.equals(node1.getIp())) {
				return true;
			}
		}
		return false;	
	}	
}
